package edu.iscas.appcheckclient.service.network;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;

/**
 * Created by tangbo on 16/9/5.
 */
public class HttpUtil {
    private static final String TAG = HttpUtil.class.getSimpleName();
    private static RequestManager requestManager;
    private static HttpUtil httpUtil;
    private static String serverAddress = "";
    private Context context;
    private HttpUtil(Context ctx, String serverAddress){
        this.context = ctx;
        HttpUtil.serverAddress = serverAddress;
        requestManager = RequestManager.getInstance(ctx);
    }
    public static synchronized HttpUtil getInstance(Context ctx, String serverAddress){
        if(httpUtil == null){
            httpUtil = new HttpUtil(ctx, serverAddress);
        }
        return httpUtil;
    }
    public void jsonArrayRequest(int method, String httpAPI, Response.Listener listener, Response.ErrorListener errorListener)
    {
        String url = serverAddress + httpAPI;
        Toast.makeText(context,url, Toast.LENGTH_SHORT).show();
        JsonArrayRequest arrayRequest = new JsonArrayRequest(method, url, listener,errorListener);
        requestManager.addToRequestQueue(arrayRequest);
    }
    public void stringRequest(int method, String httpAPI, Response.Listener listener, Response.ErrorListener errorListener)
    {
        String url = serverAddress + httpAPI;
        Log.d(TAG, url);
        Toast.makeText(context,url, Toast.LENGTH_SHORT).show();
        StringRequest stringRequest = new StringRequest(method, url,  listener, errorListener);
        requestManager.addToRequestQueue(stringRequest);
    }
}
